package ch.zbw.timeline.presentation.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


/**
 * Date and time formatter with singleton pattern
 * 
 * @author Daniel Stefanovic
 * @version 19.01.2012
 * 
 */
public enum Formatter {

	INSTANCE;
	
	private static final String TIME_FORMAT = "HH:mm";
	private static final String DATE_FORMAT = "dd.MM.yyyy";
	private static final String TIMESTAMP_FORMAT = "dd.MM.yyyy - HH:mm:ss";
	
	private SimpleDateFormat formatter = new SimpleDateFormat();
	
	/**
	 * Formats a time
	 * 
	 * @param time time to format
	 * @return formatted time
	 */
	public String formatTime(Date time){
		formatter.applyPattern(TIME_FORMAT);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.format(time);
	}
	
	/**
	 * Formats a date
	 * 
	 * @param date date to format
	 * @return formatted time
	 */
	public String formatDate(Date date){
		formatter.applyPattern(DATE_FORMAT);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.format(date);
	}
	
	/**
	 * Formats a date to a timestamp
	 * 
	 * @param date date to format
	 * @return formatted date
	 */
	public String formatTimestamp(Date date){
		formatter.applyPattern(TIMESTAMP_FORMAT);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.format(date);
	}
	
	/**
	 * Parse a time string
	 * 
	 * @param time string to parse
	 * @return date object
	 * @throws ParseException
	 */
	public Date parseTime(String time) throws ParseException{
		formatter.applyPattern(TIME_FORMAT);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.parse(time);
	}
	
	
	/**
	 * Parse a date string
	 * 
	 * @param date string to parse
	 * @return date object
	 * @throws ParseException
	 */
	public Date parseDate(String date) throws ParseException{
		formatter.applyPattern(DATE_FORMAT);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter.parse(date);
	}
	
	/**
	 * Returns the date formatter
	 * 
	 * @return date formatter
	 */
	public SimpleDateFormat getDateFormatter(){
		formatter.applyPattern(DATE_FORMAT);
		formatter.setTimeZone(TimeZone.getDefault());
		return formatter;
	}
	
	
}
